package com.winchain.chainbill.basic.invoice.rx;

import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.basic.invoice.service.InvoiceDeferedService;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.po.TbSnd;
import com.winchain.chainbill.tbill.base.service.TbRecService;
import com.winchain.chainbill.tbill.base.service.TbSndService;
import com.winchain.chainbill.tbill.biz.TBill;
import com.winchain.chainbill.tbill.biz.TBillEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TBillInvoiceDfrUploadSaveSubscriber extends OnErrorNotImplementedSubscriber<TBillEvent<TbDct>> {
	@Autowired
	private TbRecService tbRecService;
	@Autowired
	private TbSndService tbSndService;
	@Autowired
	private InvoiceDeferedService invoiceDeferedService;

	public void onApplicationEvent(TBillEvent<TbDct> t) {
		TbDct dct = (TbDct) t.getBill().getSource();
		if (dct == null) {
			return;
		}
		if (!dct.isUploadInvoiceDefer()) {
			return;
		}
		String dfrType = "2";
		String bizId = dct.getDctId();
		int cusDfrDays = dct.getCusDfrDays();
		TbRec rec = (TbRec) this.tbRecService.selectByKey(dct.getBillId());
		TbSnd snd = this.tbSndService.selectByBillId(rec.getRootId());
		String grpId = rec.getGrpId();
		String senderId = snd.getBuyerId();
		String applyDate = dct.getDctDate();
		int dctDays = dct.getDctDays().intValue();

		this.invoiceDeferedService.saveInvDfr(bizId, dfrType, cusDfrDays, grpId, senderId, applyDate, dctDays);
	}
}
